<?php


namespace App\Http\Controllers\Web;

use App\Models\Order;
use Illuminate\Database\Query\Builder;

/**
 * 订单
 * Class OrderController
 * @package App\Http\Controllers\Web
 */
class OrderController extends Controller
{
    /** @var Builder */
    protected $builder;

    /**
     * 时间搜索类型
     * @var array
     */
    public static $queryTimeTypes = [
        1 => '按下单时间',
        2 => '按付款时间',
        3 => '按接单时间',
        4 => '按完成时间',
    ];
    /**
     * 待接单
     */
    public function waitReceive()
    {
        /** @var array 订单状态包含 $order_status_array */
        $order_status_array = [1];
        /** @var array 支付状态包含 $paid_array */
        $paid_array = [1];

        $time_query = self::$queryTimeTypes;
        $data = $this->getList($order_status_array, $paid_array,$time_query);
        return view('order.list', $data);
    }

    /**
     * 配送中
     */
    public function distribution()
    {
        /** @var array 订单状态包含 $order_status_array */
        $order_status_array = [2];
        /** @var array 支付状态包含 $paid_array */
        $paid_array = [1];

        $time_query = self::$queryTimeTypes;
        $data = $this->getList($order_status_array, $paid_array,$time_query);
        return view('order.list', $data);
    }

    /**
     * 完成
     */
    public function finish()
    {
        /** @var array 订单状态包含 $order_status_array */
        $order_status_array = [3];
        /** @var array 支付状态包含 $paid_array */
        $paid_array = [1];

        $time_query = self::$queryTimeTypes;
        $data = $this->getList($order_status_array, $paid_array,$time_query);
        return view('order.list', $data);
    }

    /**
     * 所有订单
     */
    public function all()
    {
        /** @var array 订单状态包含 $order_status_array */
        $order_status_array = [0, 1, 2, 3];
        /** @var array 支付状态包含 $paid_array */
        $paid_array = [0, 1];

        $time_query = self::$queryTimeTypes;
        $data = $this->getList($order_status_array, $paid_array,$time_query);
        return view('order.list', $data);
    }

    public function ticket()
    {
        $query = $this->query;
        $queryData = $query;
        $builder = Order::query();

        if($query['order_id']){
            $builder->where('order_id', $query['order_id']);
        }

        $builder
            ->whereType('3')
            ->where('paid', 1)
            ->orderBy('created_at', 'desc');

        $pager = $builder->paginate($this->pageNum);

        return view('order.ticket', [
            'queryData' => $queryData,
            'pager' => $pager,
        ]);
    }

    /**
     * 订单列表
     * @param $order_status array
     * @param $refund_status array
     * @param $paid
     * @return array
     */
    public function getList($order_status, $paid, $time_query=null)
    {
        $query = $this->query;
        $queryData = $query;
        $this->builder = Order::query();

        if($query['order_id']){
            $this->builder->where('order_id', $query['order_id']);
        }

        if($query['school_name']){
            $this->builder->whereHas('school', function($q)use($query){
                $q->where('name', 'like', "%{$query['school_name']}%");
            });
        }else{
            $this->builder->with('school');
        }
        $this->builder->with('escort')->whereIn('status', $order_status)
            ->whereIn('paid', $paid)
            ->orderBy('created_at', 'desc');

        $this->getBuilderByTimeType($query['time_type'], $query['start_time'], $query['end_time'], '');
        $queryData['time_types'] = $time_query;
        $pager = $this->builder->paginate($this->pageNum);

        return [
            'queryData' => $queryData,
            'pager' => $pager,
        ];
    }

    /**
     * 根据时间类型匹配搜索
     * @param $timeType
     * @param $startTime
     * @param $endTime
     * @param $pref
     */
    private function getBuilderByTimeType($timeType, $startTime, $endTime, $pref)
    {
        $column = '';
        switch ($timeType) {
            case 1:
                $column = $pref . 'created_at';
                break;
            case 2:
                $column = $pref . 'pay_time';
                break;
            case 3:
                $column = $pref . 'shop_deliver_time';
                break;
            case 4:
                $column = $pref . 'escort_receive_time';
                break;
            case 5;
                $column = $pref . 'finish_time';
                break;
        }

        if ($column) {
            if ($startTime) {
                if ($column == $pref . 'created_at') {
                    $startTime = strtotime($startTime);
                }
                $this->builder->where($column, '>=', $startTime);
            }
            if ($endTime) {
                if ($column == $pref . 'created_at') {
                    $endTime = strtotime($endTime);
                }
                $this->builder->where($column, '<=', $endTime);
            }
        }
    }
}
